package cn.webrx.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-05-26 13:05
 */
@Configuration
public class ShiroConfig {
    //ShiroFilterFactoryBean
    @Bean(name = "shiroFilterFactoryBean")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("dsm") DefaultWebSecurityManager dsm) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();

        //添加shiro的内置过滤器
        //anon：无需认证就可以访问
        //authc 必须认证才能访问
        //user 必须拥有 ，让住我 功能才能用
        //perms 拥有对某个资源权限才能访问
        //role 拥有某个角色权限才能访问

        Map<String, String> map = new HashMap<>();
        //map.put("/user/add", "authc");
        //map.put("/user/update", "authc");
        //使用通配符
        map.put("/user/*","authc");
        bean.setFilterChainDefinitionMap(map);


        bean.setLoginUrl("/login");
        //设置安全管理器
        bean.setSecurityManager(dsm);
        return bean;
    }

    //DefaultWebSecurityManager
    @Bean(name = "dsm")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager dsm = new DefaultWebSecurityManager();
        dsm.setRealm(userRealm);
        return dsm;
    }

    //Realm
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }
}
